<?php

/**
 * Users controller
 *
 * @package       AgencyCMS
 * @subpackage    AgencyCMS.users
 * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * AgencyUsersController
 */
class AgencyUsersController extends AppController {

    /**
     * @var string Class name
     */
    var $name = 'AgencyUsers';

    /**
     * Attempt to log the user in
     */
    function admin_login()
    {}

    /**
     * List all current users in the CMS
     */
    function admin_index()
    {

        $this->paginate = array(
            'order' => array(
                'AgencyUser.username' => 'asc'
            )
        );
        $agencyUsers = $this->paginate('AgencyUser');

        $this->set(compact('agencyUsers'));

    }

    /**
     * Add a new user account
     */
    function admin_add()
    {

        if (!empty($this->data)) {

            // hash the password via the Auth component
            $this->data['AgencyUser']['password'] = $this->Auth->password(
                $this->data['AgencyUser']['new_password']
            );

            if ($this->AgencyUser->save($this->data)) {
                $this->Session->setFlash(
                    __('New user added successfully', true)
                );
                $this->redirect(array(
                    'controller' => 'agency_users',
                    'action'     => 'index',
                    'admin'      => true
                ));
            }

        }

    }

    /**
     * Edit an existing user account
     * @param int $id
     */
    function admin_edit($id)
    {

        $agencyUser = $this->AgencyUser->read(null, $id);

        if (!empty($this->data)) {

            // if user password was reset, hash the password via the Auth component
            if (isset($this->data['AgencyUser']['new_password'])) {
                $this->data['AgencyUser']['password'] = $this->Auth->password(
                    $this->data['AgencyUser']['new_password']
                );
                $setPassword = true;
            } else {
                $setPassword = false;
            }

            if ($this->AgencyUser->save($this->data)) {
                if ($setPassword) {
                    $this->Session->setFlash(
                        __('Password changed successfully', true)
                    );
                } else {
                    $this->Session->setFlash(
                        __('User account saved successfully', true)
                    );
                }
                $this->redirect(array(
                    'controller' => 'agency_users',
                    'action'     => 'index',
                    'admin'      => true
                ));
            } else {
                // merge POSTed data with data fetch from database, otherwise the
                // form not submitted will be empty
                $this->data['AgencyUser'] = array_merge(
                    $agencyUser['AgencyUser'],
                    $this->data['AgencyUser']
                );
            }

        } else {
            $this->data = $agencyUser;
        }

        $this->set(compact('agencyUser'));

    }

    /**
     * Delete an existing user account
     * @param int $id
     */
    function admin_delete($id)
    {

        // users cannot delete their own account
        if ($id == $this->Auth->user('id')) {
            $this->cakeError('error404');
        }

        $agencyUser = $this->AgencyUser->read(null, $id);

        if (!empty($this->data)) {
            if ($this->data['AgencyUser']['delete']) {
                $this->AgencyUser->del($id);
                $this->Session->setFlash(
                    __('User account deleted', true)
                );
                $this->redirect(array(
                    'action'     => 'index',
                    'admin'      => true
                ));
            }
        }

        $this->set(compact('agencyUser'));

    }

}